﻿package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Public.*;
    import com.gengine.debug.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.postProcess.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.relive.*;
    import mortal.game.view.relive.parts.*;
    import mortal.mvc.core.*;

    public class ReliveController extends Controller
    {
        private var _sceneUpdated:Boolean = false;
        private var _killerInfo:SEntityKillerInfo;
        private var _relieveTimer:Timer;
        private var _curRelivePanel:RelivePanelBase;
        public static var serverRoleDeadTime:int = -1111111;

        public function ReliveController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            super.initServer();
            cache.role.roleEntityInfo.addEventListener(EntityInfoEventName.Relive, this.onRoleRelived);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicEntityKillerInfo, this.onRoleDead);
            Dispatcher.addEventListener(EventName.Role_HideRelivePanel, this.hideRelivePanelHandler);
            Dispatcher.addEventListener(EventName.Revival_ProRevival, this.askPropRevivalHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            Dispatcher.addEventListener(EventName.StageResize, this.stageResizeHandler);
            return;
        }// end function

        private function stageResizeHandler(event:DataEvent) : void
        {
            if (this._curRelivePanel != null && !this._curRelivePanel.isHide)
            {
                this._curRelivePanel.onStageResize();
            }
            return;
        }// end function

        private function sceneUpdateHandler(event:DataEvent) : void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            this._sceneUpdated = true;
            if (this._killerInfo != null)
            {
                this.roleDead();
            }
            return;
        }// end function

        private function askPropRevivalHandler(event:DataEvent) : void
        {
            var _loc_2:* = cache.pack.backPackCache.getItemCountByCategoryAndType(EGroup._EGroupProp, EProp._EPropProp, EPropType._EPropTypeRevival);
            if (cache.vip.freeRevivalTimes == 0 && _loc_2 <= 0)
            {
                GameController.guide.buyRevivalProp();
                return;
            }
            GameProxy.sceneProxy.relive(ERevival._ERevivalSitu);
            return;
        }// end function

        private function hideRelivePanelHandler(event:DataEvent) : void
        {
            this.hideRelivePanel();
            return;
        }// end function

        public function stageResize() : void
        {
            return;
        }// end function

        public function onRoleRelived(event:Event = null) : void
        {
            serverRoleDeadTime = -1111111;
            if (event != null)
            {
                Log.debug("复活： 收到服务器的角色复活的消息");
            }
            this.hideRelivePanel();
            PostProcess.instance.enableDeathGreyFilter(false);
            this.removeRelieveTimer();
            return;
        }// end function

        private function removeRelieveTimer() : void
        {
            if (this._relieveTimer != null)
            {
                this._relieveTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.timeToShowRelievePanel);
                this._relieveTimer.stop();
                this._relieveTimer = null;
            }
            return;
        }// end function

        private function onRoleDead(param1:MessageBlock) : void
        {
            serverRoleDeadTime = getTimer();
            Log.debug("复活： 收到服务器的死亡消息了！1");
            this.removeRelieveTimer();
            if (GameMapUtil.curMapState.isArenaCopy)
            {
                Log.debug("复活： 收到服务器的死亡消息了！2");
                return;
            }
            if (GameMapUtil.curMapState.isAutoArenaMap)
            {
                return;
            }
            if (GameMapUtil.curMapState.isHeroArenaMap)
            {
                return;
            }
            if (CopyUtil.isInChallengeCopy)
            {
                Log.debug("复活： 收到服务器的死亡消息了！3");
                return;
            }
            this._killerInfo = param1.messageBase as SEntityKillerInfo;
            this.roleDead();
            return;
        }// end function

        private function getCD() : int
        {
            var _loc_3:int = 0;
            if (this._killerInfo == null || this._killerInfo.revivalCd == null)
            {
                return 0;
            }
            var _loc_1:* = this._killerInfo.revivalCd;
            var _loc_2:* = int.MIN_VALUE;
            for each (_loc_3 in _loc_1)
            {
                
                if (_loc_3 > _loc_2)
                {
                    _loc_2 = _loc_3;
                }
            }
            return _loc_2;
        }// end function

        public function roleDead() : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (this._killerInfo == null)
            {
                this._killerInfo = new SEntityKillerInfo();
                this._killerInfo.name = "";
            }
            if (!this._sceneUpdated)
            {
                Log.debug("复活： 收到服务器的死亡消息了！4");
                return;
            }
            var _loc_1:* = this.getCD();
            if (_loc_1 <= 0)
            {
                if (_loc_2 == ERevival._ERevivalForcePoint)
                {
                    GameProxy.sceneProxy.relive(ERevival._ERevivalForcePoint);
                    return;
                }
                if (AutoFightAI.isWorking)
                {
                    if (cache.autoFight.isAutoRelieve)
                    {
                        _loc_2 = GameSceneUtil.getReliveType();
                        _loc_3 = cache.pack.backPackCache.getItemCountByItemCode(GameConst.RelivePropItemId);
                        _loc_4 = cache.vip.getLeftFreeTimesByType(EVipRecordType._EVipRecordTypeRelive);
                        if (GuideAutoFightWin.isWorking())
                        {
                            _loc_2 = ReliveType.City;
                            AIManager.cancelAll();
                            GuideAutoFightWin.instance.hide();
                        }
                        switch(_loc_2)
                        {
                            case ReliveType.General:
                            case ReliveType.Prop:
                            {
                                if (_loc_3 > 0 || _loc_4 != 0)
                                {
                                    GameProxy.sceneProxy.relive(ERevival._ERevivalSitu);
                                }
                                else
                                {
                                    GameController.guide.buyRevivalProp();
                                }
                                break;
                            }
                            case ReliveType.City:
                            {
                                GameProxy.sceneProxy.relive(ERevival._ERevivalPoint);
                                break;
                            }
                            case ReliveType.Force:
                            {
                                GameProxy.sceneProxy.relive(ERevival._ERevivalForce);
                                break;
                            }
                            case ReliveType.FailCopy:
                            {
                                GameProxy.sceneProxy.relive(ERevival._ERevivalExitCopy);
                                break;
                            }
                            default:
                            {
                                break;
                            }
                        }
                        this.removeRelieveTimer();
                        this._relieveTimer = new Timer(1200, 1);
                        this._relieveTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.timeToShowRelievePanel);
                        this._relieveTimer.start();
                        Log.debug("复活： 收到服务器的死亡消息了！5");
                        return;
                    }
                }
            }
            if (this._curRelivePanel != null && !this._curRelivePanel.isHide)
            {
                Log.debug("复活： 收到服务器的死亡消息了！6");
                return;
            }
            this.timeToShowRelievePanel(null);
            return;
        }// end function

        private function timeToShowRelievePanel(event:TimerEvent) : void
        {
            if (this._killerInfo == null)
            {
                Log.debug("复活： 收到服务器的死亡消息了！7");
                return;
            }
            PostProcess.instance.enableDeathGreyFilter(true);
            this.showRelivePanel(this._killerInfo);
            this._killerInfo = null;
            return;
        }// end function

        private function showRelivePanel(param1:SEntityKillerInfo) : void
        {
            var _loc_2:* = GameSceneUtil.getReliveType();
            this.hideRelivePanel();
            var _loc_3:* = this.getCD();
            switch(_loc_2)
            {
                case ReliveType.General:
                {
                    this._curRelivePanel = UICompomentPool.getUICompoment(RelivePanelGeneral);
                    break;
                }
                case ReliveType.Prop:
                {
                    this._curRelivePanel = UICompomentPool.getUICompoment(RelivePanelLocal);
                    break;
                }
                case ReliveType.City:
                {
                    this._curRelivePanel = UICompomentPool.getUICompoment(RelivePanelCity);
                    break;
                }
                case ReliveType.Force:
                {
                    this._curRelivePanel = UICompomentPool.getUICompoment(RelivePanelForce);
                    break;
                }
                case ReliveType.FailCopy:
                {
                    break;
                }
                case ReliveType.OnlyCDOverForceRelivePoint:
                {
                    this._curRelivePanel = UICompomentPool.getUICompoment(ReliveCDOverForceRelivePoint);
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (this._curRelivePanel != null)
            {
                this._curRelivePanel.show();
                this._curRelivePanel.killerInfo = param1;
                this._curRelivePanel.setLeftTime(_loc_3);
            }
            else
            {
                Log.debug("复活： 收到服务器的死亡消息了！9, 复活类型是：" + _loc_2);
            }
            Log.debug("复活： 收到服务器的死亡消息了！8");
            return;
        }// end function

        private function hideRelivePanel() : void
        {
            if (this._curRelivePanel == null)
            {
                return;
            }
            if (!this._curRelivePanel.isHide)
            {
                Log.debug("复活： 收到服务器的角色复活的消息, 隐藏复活面板");
                this._curRelivePanel.hide();
            }
            this._curRelivePanel.dispose(false);
            this._curRelivePanel = null;
            return;
        }// end function

        private function onAlertOkHandler(param1:int) : void
        {
            return;
        }// end function

    }
}
